package com.study.example.gameserver.module.currency;

import com.sparrow.event.annotation.EventReceiver;
import com.sparrow.example_message.base.resp.RespCurrencyInfoPacket;
import com.study.example.gameserver.module.backpack.controller.BackPackController;
import com.study.example.gameserver.module.currency.entity.PlayerCurrency;
import com.study.example.gameserver.module.player.domain.PlayerDataType;
import com.study.example.gameserver.module.player.entity.Player;
import com.study.example.gameserver.module.player.event.PlayerInitEvent;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

/**
 * 货币
 */
@Component
public class PlayerCurrencyService {
    private static Logger logger = LoggerFactory.getLogger(PlayerCurrencyService.class);

    @EventReceiver(order = 1)
    public void init(PlayerInitEvent event) {
        Player player = event.getPlayer();
        sendCurrencyInfo(player);
        logger.info("player{} 货币初始化通知", player.getPlayerId());
    }

    public void sendCurrencyInfo(Player player) {
        PlayerCurrency playerCurrency = player.getData(PlayerDataType.Currency);
        Map<Integer, Long> currencyMap = new HashMap<>();
        for (Map.Entry<Integer, Long> e : playerCurrency.getDataMap().entrySet()) {
            currencyMap.put(e.getKey(), e.getValue());
        }
        RespCurrencyInfoPacket packet = new RespCurrencyInfoPacket();
        packet.setCurrencyMap(currencyMap);
        player.sendPacket(packet);
    }
}
